home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1129 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.1 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Better template syntax?
  5. Date: 17 Apr 1996 14:57:38 GMT
  6. Organization: Technical University of Berlin
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <3174CDC5.3AF6@cs.tu-berlin.de>
  9. References: <31741E6C.53CA@cs.tu-berlin.de> <AUSTERN.96Apr16155031@isolde.mti.sgi.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Mime-Version: 1.0
  12. Content-Type: text/plain; charset="us-ascii"
  13. Content-Transfer-Encoding: 7bit
  14. X-Nntp-Posting-Host: 130.149.17.236
  15. X-Mailer: Mozilla 2.0 (Win95; I)
  16. X-Lines: 95
  17. Content-Length: 4200
  18. Originator: clamage@taumet
  19.  
  20. Matt Austern wrote:
  21. > In article <31741E6C.53CA@cs.tu-berlin.de> Roman Lechtchinsky
  22. > <wolfro@cs.tu-berlin.de> writes:
  23. > > sorry for being curious but I'm only human... My question is: when declaring
  24. > > templates, why isn't it required to declare what one expects of the
  25. > > template's parameters? Let's create a template function:
  26. > >
  27. > > template<class T> bool equal( const T& t1, const T& t2 )
  28. > > {
  29. > >   return t1==t2;
  30. > > }
  31. > >
  32. > > Now, personally I would prefer to write something like:
  33. > >
  34. > > template<class T { bool operator==( const T& t ) const; }> ...
  35. > Specifying something like this correctly turns out to be extremely
  36. > difficult: it's very hard to come up with syntax that neither
  37. > overspecifies nor underspecifies the constraints.  In this particular
  38. > case, for example: do you really care whether operator== takes a const
  39. > T&, as opposed to a T?  
  40.  
  41. Sure I do. If it takes a T the template needs access to the copy constructor. 
  42. This is something one tends to forget about - the compiler wouldn't let you 
  43. if you had to declare this fact.
  44.  
  45. > Does it matter that it returns bool, instead
  46. > of something (like int) that is freely convertable to bool?  
  47.  
  48. It depends on what this something is and whether you really want it to be 
  49. converted to bool or not ( in the case of operator== you almost certainly do; 
  50. maybe it's a bad example ). No problem if it is really int. If it is a class 
  51. which can be converted to bool or to int or even to a pointer then the 
  52. programmer should at least know which conversions might occur. Usually 
  53. templates are not documented that good.
  54. As a matter of fact, my definition could accept an operator== with a 
  55. different return type. When calling it the return value would be converted to 
  56. bool prior to any other conversions which would give the programmer at least 
  57. some control.
  58.  
  59. > Does it
  60. > matter whether this is T::operator==(T), or ::operator==(T,T), or any
  61. > of a dozen other minor variations?
  62.  
  63. Note that this problem occurs only with operators. Again, the two operators 
  64. you mention have different semantics: the second one copies two objects 
  65. whereas the first one copies only one. 
  66.  
  67. > More realistic cases are even harder to deal with.  Consider, for
  68. > example, the for_each algorithm from STL:
  69. > template <class InputIterator, class Function>
  70. > Function for_each(InputIterator first, InputIterator last, Function f) {
  71. >     while (first != last) f(*first++);
  72. >     return f;
  73. > }
  74. > The constraints are as follows:
  75. >   There must be either a built-in operator!=, or a global operator!=,
  76. >     or a member function InputIterator::operator!=, such that applying
  77. >     operator!= to two InputIterators is well-defined and yields something
  78. >     that's convertable to bool.
  79. >   Operators ++ and * are defined such that *InputIterator++ yields an
  80. >     rvalue of some type.
  81. >   An object of type Function has an operator() that takes an argument
  82. >     of the type returned by *InputIterator++, or at least a type to
  83. >     which the return value of *InputIterator++ can be converted.
  84. >     The return value of operator() is unimportant.
  85. > Notice what's tricky here: the important constraints are relationships
  86. > between different types, some of which (like the return value of
  87. > InputIterator++, and the return value of *InputIterator++) aren't
  88. > named as template parameters.  This rapidly becomes a problem in
  89. > formal specification.
  90.  
  91. Yes, sure. I could try to design a class  which messes the whole thing up 
  92. even though it provides everything the function needs. This rapidly becomes a 
  93. problem in application development :-) Essentially, templates are macros. One 
  94. can do a lot of things with macros. But should they be done? Maybe STL the 
  95. design of STL would be clearer if everything had to be declared...
  96.  
  97. > This issue is something that has been thought about, and there's some
  98. > discussion of it in D&E.
  99.  
  100. Yes, there is ( I think so, at least; I don't remember who has my copy but 
  101. its not me ). However, in many threads about templates I've read something 
  102. like "now the semantics of templates are better understood". I'd like to hear 
  103. what people think about them in the light of this new understanding.
  104.  
  105. Bye
  106.  
  107. Roman
  108.  
  109.  
  110. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  111. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  112. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  113. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  114. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  115.